package com.mashen.client;

import com.alibaba.fastjson.JSONObject;
import com.mashen.client.model.AccessToken;
import com.mashen.client.model.ButtonRequest;
import com.mashen.client.model.MediaResult;
import com.mashen.client.model.QRCodeRequest;
import com.mashen.client.model.QRCodeTicket;
import com.mashen.client.model.TextKfMessageRequest;
import com.mashen.client.model.UserInfo;
import com.mashen.util.ConfigUtil;
import com.mashen.util.Constant;
import com.mashen.util.HttpUtil;

public class WeiXinClient {
	private static final String GET="GET";
	private static final String POST="POST";
	private static final String GET_TOKEN="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
	private static final String POST_BUTTON="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s";
	private static final String GET_USERINFO="https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN";
	private static final String POST_QRCODETK=" https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=%s";
	private static final String POST_MEDIA="https://api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s";
	private static final String POST_KFMSG="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s";
	private static AccessToken token;
	/**
	 * 获取接口调用凭证
	 * @return
	 * @throws Throwable 
	 */
	public static void getToken() throws Throwable{
		if(token!=null&&!token.isExpires()){
			return;
		}
		System.out.println("重新获取token");
		//重新获取token
		String jsonstr=HttpUtil.sendHttpRequest(String.format(GET_TOKEN,
				ConfigUtil.getValue("appID"),
				ConfigUtil.getValue("appsecret")), 
				GET, null, Constant.CHARSET);
		token=JSONObject.parseObject(jsonstr, 
				AccessToken.class);
	}
	/**
	 * 创建菜单
	 * @throws Throwable
	 */
	public static void createButtons() throws Throwable{
		getToken();
		String buttonjsonstr=ButtonRequest.getDefaultButton();
		String jsonstr=HttpUtil.sendHttpRequest(String.format(POST_BUTTON,
				token.getAccess_token()), 
				POST, buttonjsonstr, Constant.CHARSET);
	}
	/**
	 * 获取用户基本信息
	 * @param openid
	 * @return
	 * @throws Throwable
	 */
	public static UserInfo getUserInfo(String openid) throws Throwable{
		getToken();
		String jsonstr=HttpUtil.sendHttpRequest(
				String.format(GET_USERINFO,
				token.getAccess_token(),openid), 
				GET,null, Constant.CHARSET);
		return JSONObject.parseObject(jsonstr,
                  UserInfo.class);
	}
	/**
	 * 获取二维码ticket
	 * @param scendid
	 * @return
	 * @throws Throwable
	 */
	public static QRCodeTicket getQRCodeTicket(long scendid) 
			throws Throwable{
		QRCodeRequest request=new QRCodeRequest();
		request.setSceneID(scendid);
		getToken();
		String reqjson=JSONObject.toJSONString(request);
		String jsonstr=HttpUtil.sendHttpRequest(
				String.format(POST_QRCODETK,
				token.getAccess_token()), 
				POST,reqjson, Constant.CHARSET);
		return JSONObject.parseObject(jsonstr,
				QRCodeTicket.class);
		
	}
	
	public static MediaResult uploadMedia(String type,String filepath)
		throws Throwable{
		getToken();
		String jsonstr=HttpUtil.upload(String.format(POST_MEDIA,
				token.getAccess_token(),type), 
				filepath, Constant.CHARSET);
		return JSONObject.parseObject(jsonstr,MediaResult.class);
	}
	
	/**
	 * 发送客服消息
	 * @throws Throwable 
	 */
	public static void sendkfMessage(String openid,
			String content) throws Throwable{
		getToken();
		TextKfMessageRequest request=
				new TextKfMessageRequest();
		request.setTouser(openid);
		request.setMsgtype(Constant.MSG_TYPE_TEXT);
		request.setContent(content);
		String jsonstr=HttpUtil.sendHttpRequest(
				String.format(POST_KFMSG,
				token.getAccess_token()), 
				POST,JSONObject.toJSONString(request),
				Constant.CHARSET);
		System.out.println(jsonstr);
		
	}
	public static void main(String[] args) 
			throws Throwable {
		//AccessToken token=getToken();
		//System.out.println(token.getAccess_token());
		//System.out.println(token.getExpires_in());
		//createButtons();
		QRCodeTicket tk=getQRCodeTicket(1);
		System.out.println(tk.getTicket());
	}
}
